package com.garrett.demo.sys.dao;

import com.garrett.demo.sys.entity.Account;
import com.garrett.demo.sys.entity.SimsStudent;

import java.io.*;
import java.util.Arrays;

public class UserDataBaseDemo implements UserDao {
    private SimsStudent[] students = new SimsStudent[3];
    private Account[] accounts = new Account[3];

    public void setAccounts(Account[] accounts) {
        this.accounts = accounts;
    }

    public Account[] getAccounts() {
        return accounts;
    }

    public void setStudents(SimsStudent[] students) {
        this.students = students;
    }

    public SimsStudent[] getStudents() {
        return students;
    }

    //new update 12.13
    //记录1
    SimsStudent recordFormat1(String row) {

        SimsStudent studentInfo = null;

        int beginIndex = row.indexOf("{") + 1;
        int endIndex = row.indexOf("}");

        String content = row.substring(beginIndex, endIndex);
        String[] split = content.split(",");
        String[] fields = Arrays.stream(split).map(String::trim).toArray(String[]::new);

        if (fields.length > 0) {
            studentInfo = new SimsStudent();
            studentInfo.setStudentId(fields[0]);
            studentInfo.setStudentName(fields[1]);
            studentInfo.setEngName(fields[2]);
            studentInfo.setIdCardNo(fields[3]);
            studentInfo.setMobilePhone(fields[4]);
            studentInfo.setGender(fields[5]);
            studentInfo.setHeight(Integer.parseInt(fields[6]));
            studentInfo.setWeight(Integer.parseInt(fields[7]));
            studentInfo.setNation(fields[8]);
            studentInfo.setPolitical(fields[9]);

        }
        return studentInfo;
    }

    //通过学号获取学生信息
    @Override
    public SimsStudent getStudentById(String stuId) {//new update 12.13

        String path = "/work_2021/212006311/src/resources/studentInfo.txt";
        File file = new File(path);
        SimsStudent studentInfo = null;

        try {
            if (file.isFile() && file.exists()) {

                //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
                BufferedReader bufferedReader = new BufferedReader(read);

                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {

                    studentInfo = recordFormat1(lineTxt);

                    if (studentInfo.getStudentId().equals(stuId)) {
                        read.close();
                        return studentInfo;
                    }

                }

                read.close();
            } else {

                System.out.println("找不到指定的文件");
                return null;

            }
        } catch (Exception e) {

            System.out.println("读取文件内容出错");
            return null;

        }
        return null;
    }

    //记录2
    Account recordFormat2(String row) {//new update 12.15

        Account accountInfo = null;

        int beginIndex = row.indexOf("{") + 1;
        int endIndex = row.indexOf("}");

        String content = row.substring(beginIndex, endIndex);
        String[] split = content.split(",");
        String[] fields = Arrays.stream(split).map(String::trim).toArray(String[]::new);

        if (fields.length > 0) {

            accountInfo = new Account();
            accountInfo.setId(fields[0]);
            accountInfo.setAccount(fields[1]);
            accountInfo.setPassword(fields[2]);

        }

        return accountInfo;
    }

    //通过账号获取用户信息
    @Override
    public Account getUserByAccount(String account) {//new update 12.15

        String path = "/work_2021/212006311/src/resources/accountInfo.txt";

        File file = new File(path);
        Account accountInfo = null;

        try {
            if (file.isFile() && file.exists()) {
                //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;

                while ((lineTxt = bufferedReader.readLine()) != null) {

                    accountInfo = recordFormat2(lineTxt);
                    if (accountInfo.getAccount().equals(account)) {

                        read.close();
                        return accountInfo;

                    }

                }
                read.close();
            } else {

                System.out.println("找不到指定的文件");
                return null;
            }
        } catch (Exception e) {

            System.out.println("读取文件内容出错");
            // e.printStackTrace();
            return null;
        }
        return null;
    }

    //添加账户
    @Override
    public int addAccount(Account account) {

        String path = "/work_2021/212006311/src/resources/accountInfo.txt";

        File file = new File(path);
        OutputStreamWriter osw = null;

        try {
            // 创建基于文件的输出流
            FileOutputStream fos = new FileOutputStream(file, true);

            // 指定编码格式，以免读取时中文字符异常
            osw = new OutputStreamWriter(fos, "UTF-8");
            osw.write(account.toString() + "\n");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != osw) {
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

    //创建用户
    @Override
    public int createUser(SimsStudent userInfo) {

        String path = "/work_2021/212006311/src/resources/studentInfo.txt";
        File file = new File(path);
        OutputStreamWriter osw = null;

        try {
            // 创建基于文件的输出流
            FileOutputStream fos = new FileOutputStream(file, true);

            // 指定编码格式，以免读取时中文字符异常
            osw = new OutputStreamWriter(fos, "UTF-8");
            osw.write(userInfo.toString() + "\n");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != osw) {
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

    @Override
    public SimsStudent[] getAllStudent() {

        String path = "/work_2021/212006311/src/resources/studentInfo.txt";
        File file = new File(path);
        int i = 0;

        try {
            if (file.isFile() && file.exists()) {//判断文件是否存在

                InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    i++;
                }

                read.close();
                read = new InputStreamReader(new FileInputStream(file), "UTF-8");
                bufferedReader = new BufferedReader(read);

                lineTxt = null;
                SimsStudent[] stu = new SimsStudent[i];
                i = 0;

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    stu[i] = recordFormat1(lineTxt);
                    i++;
                }

                read.close();


                return stu;
            } else {

                System.out.println("找不到指定的文件");
                return null;

            }
        } catch (Exception e) {

            return null;

        }
    }

    public Account[] getAllAccount() {

        String path = "/work_2021/212006311/src/resources/accountInfo.txt";
        File file = new File(path);
        int i = 0;

        try {
            if (file.isFile() && file.exists()) {
                //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    i++;
                }

                read.close();

                read = new InputStreamReader(new FileInputStream(file), "UTF-8");
                bufferedReader = new BufferedReader(read);

                lineTxt = null;
                Account[] accounts = new Account[i];
                i = 0;

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    accounts[i] = recordFormat2(lineTxt);
                    i++;
                }
                read.close();
                return accounts;
            } else {
                System.out.println("找不到指定的文件");
                return null;
            }
        } catch (Exception e) {
            System.out.println("未有注册用户");
            // e.printStackTrace();
            return null;
        }
    }

    //根据学号删除用户
    @Override
    public int delAccountById(String stuId) {
        //new update 12.10

        String path = "/work_2021/212006311/src/resources/accountInfo.txt";
        File file = new File(path);
        int i = 0;
        Boolean f = true;

        try {
            if (file.isFile() && file.exists()) {
                //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    i++;
                }
                read.close();
                read = new InputStreamReader(new FileInputStream(file), "UTF-8");

                bufferedReader = new BufferedReader(read);
                lineTxt = null;

                Account[] accounts = new Account[i];
                i = 0;

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    accounts[i] = recordFormat2(lineTxt);
                    i++;
                }

                read.close();
                for (int j = 0; j < i; j++) {
                    if (accounts[j].getAccount().equals(stuId)) {
                        f = false;
                    }
                }

                if (f) {
                    return 0;
                } else {

                    file.delete();
                    file.createNewFile();
                    OutputStreamWriter osw = null;

                    try {
                        // 创建基于文件的输出流
                        FileOutputStream fos = new FileOutputStream(file, true);
                        // 指定编码格式，以免读取时中文字符异常
                        osw = new OutputStreamWriter(fos, "UTF-8");

                        for (int j = 0; j < i; j++) {
                            if (!accounts[j].getAccount().equals(stuId)) {
                                osw.write(accounts[j].toString() + "\n");
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (null != osw) {
                            try {
                                osw.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    return 1;
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //删除学生信息
    @Override
    public int delStudentInfo(String Account) { //new update 12.17

        String path = "/work_2021/212006311/src/resources/studentInfo.txt";
        File file = new File(path);
        int i = 0;
        Boolean f = true;

        try {
            if (file.isFile() && file.exists()) {
                //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    i++;
                }
                read.close();
                read = new InputStreamReader(new FileInputStream(file), "UTF-8");

                bufferedReader = new BufferedReader(read);
                lineTxt = null;
                SimsStudent[] students = new SimsStudent[i];
                i = 0;

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    students[i] = recordFormat1(lineTxt);
                    i++;
                }
                read.close();

                for (int j = 0; j < i; j++) {
                    if (students[j].getStudentId().equals(Account)) {
                        f = false;
                    }
                }
                if (f) {
                    return 0;
                } else {

                    file.delete();
                    file.createNewFile();
                    OutputStreamWriter osw = null;

                    try {
                        // 创建基于文件的输出流
                        FileOutputStream fos = new FileOutputStream(file, true);
                        // 指定编码格式，以免读取时中文字符异常
                        osw = new OutputStreamWriter(fos, "UTF-8");
                        for (int j = 0; j < i; j++) {
                            if (!students[j].getStudentId().equals(Account)) {
                                osw.write(students[j].toString() + "\n");
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {

                        if (null != osw) {
                            try {
                                osw.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                    return 1;
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //修改账户名
    @Override
    public int modifyAccount(Account account) {
        return 0;
    }

    //修改学生信息
    @Override
    public int modifyStudentInfo(SimsStudent userInfo) {//new update 12.20

        String path = "/work_2021/212006311/src/resources/studentInfo.txt";
        File file = new File(path);
        int i = 0, n = 0;

        try {
            if (file.isFile() && file.exists()) {//判断文件是否存在

                InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    i++;
                }

                read.close();
                read = new InputStreamReader(new FileInputStream(file), "UTF-8");

                bufferedReader = new BufferedReader(read);
                lineTxt = null;

                SimsStudent[] students = new SimsStudent[i];
                i = 0;

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    students[i] = recordFormat1(lineTxt);
                    i++;
                }

                read.close();

                for (int j = 0; j < i; j++) {
                    if (students[j].getStudentId().equals(userInfo.getStudentId())) {
                        n = j;
                        break;
                    }
                }

                students[n] = userInfo;
                file.delete();
                file.createNewFile();
                OutputStreamWriter osw = null;

                try {
                    // 创建基于文件的输出流
                    FileOutputStream fos = new FileOutputStream(file, true);
                    // 指定编码格式，以免读取时中文字符异常
                    osw = new OutputStreamWriter(fos, "UTF-8");
                    for (int j = 0; j < i; j++) {
                        osw.write(students[j].toString() + "\n");
                    }
                    return 1;
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (null != osw) {
                        try {
                            osw.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
        }
        return 0;
    }
}